#pragma once

#include "non_copy_able.h"
#include <map>
#include <queue>
#include "timer.h"
#include "mutex.h"

namespace el
{
	class EventLoop;
	class TimerQueue : noncopyable
	{
	public:  
		typedef std::multimap<TTime, Timer*>::iterator MultiMapIter;

		explicit TimerQueue(EventLoop* loop);
		~TimerQueue();
		void handleRead();

		void addTimer(const Functor& cb, TTime when, TTime interval);
		void addTimerInLoop(Timer* timer);
		static TTime now();

	private:
		EventLoop* loop_;

		struct cmp 
		{
			bool operator()(TTime a, TTime b) {
				return a > b;
			}
		};
		std::priority_queue<TTime, std::vector<TTime>, cmp> tick_queue_;
		std::multimap<TTime, Timer*> map_;

		Mutex mutex_;
	};
}